package com.health.circle.ui;

import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.RadioGroup;
import android.widget.Toast;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TabHost;

import com.health.circle.R;

import java.util.Timer;
import java.util.TimerTask;

@SuppressWarnings("deprecation")
public class MainActivity extends TabActivity implements
		OnCheckedChangeListener, OnClickListener {

	private static String TAB_TAG_CONNECT = "connect";
	private static String TAB_TAG_WAI_POINT = "waipoint";
	private static String TAB_TAG_FIND = "find";
	private static String TAB_TAB_MORE = "more";

	private Intent mFirstIntent = null;
	private Intent mSecondIntent = null;
	private Intent mThirdIntent = null;
	private Intent mFourIntent = null;

	private TabHost mTabHost = null;

	private int mCurTabId = R.id.radio_first;

	private Animation anim;

	/**
	 * 连续按两次返回键就退出
	 */
	private int keyBackClickCount = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		requestWindowFeature(Window.FEATURE_NO_TITLE);

		setContentView(R.layout.activity_main);

		initAnim();

		prepareView();

		prepareIntent();

		setupIntent();

	}

	private void initAnim() {
		anim = AnimationUtils.loadAnimation(this, R.anim.scale);
	}

	private void prepareView() {
		RadioGroup radioTab = (RadioGroup) findViewById(R.id.main_radio);
		radioTab.setOnCheckedChangeListener(this);
	}

	private void prepareIntent() {
		mFirstIntent = new Intent(this, FirstTabActivity.class);
		mSecondIntent = new Intent(this, SecondTabActivity.class);
		mThirdIntent = new Intent(this, ThirdTabActivity.class);
		mFourIntent = new Intent(this, FourTabActivity.class);
	}

	private void setupIntent() {
		mTabHost = getTabHost();
		mTabHost.addTab(buildTabSpec(TAB_TAG_CONNECT, R.string.first_tab_name,
				R.drawable.selector_tab_first, mFirstIntent));
		mTabHost.addTab(buildTabSpec(TAB_TAG_WAI_POINT,
				R.string.second_tab_name, R.drawable.selector_tab_third,
				mSecondIntent));
		mTabHost.addTab(buildTabSpec(TAB_TAG_FIND, R.string.third_tab_name,
				R.drawable.selector_tab_second, mThirdIntent));
		mTabHost.addTab(buildTabSpec(TAB_TAB_MORE, R.string.four_tab_name,
				R.drawable.selector_tab_four, mFourIntent));
	}

	private TabHost.TabSpec buildTabSpec(String tag, int resLabel, int resIcon,
			final Intent content) {
		return this.mTabHost
				.newTabSpec(tag)
				.setIndicator(getString(resLabel),
						getResources().getDrawable(resIcon))
				.setContent(content);
	}

	@Override
	public void onClick(View arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onCheckedChanged(RadioGroup group, int checkedId) {
		if (mCurTabId == checkedId) {
			return;
		}
		int index = -1;
		switch (checkedId) {
		case R.id.radio_first:
			findViewById(R.id.radio_first).startAnimation(anim);
			index = 0;
			break;
		case R.id.radio_second:
			findViewById(R.id.radio_second).startAnimation(anim);
			index = 1;
			break;
		case R.id.radio_third:
			findViewById(R.id.radio_third).startAnimation(anim);
			index = 2;
			break;
		case R.id.radio_four:
			findViewById(R.id.radio_four).startAnimation(anim);
			index = 3;
			break;
		}
		mCurTabId = checkedId;
		mTabHost.setCurrentTab(index);
	}

	@Override
	protected void onResume() {
		super.onResume();
		keyBackClickCount = 0;
	}

	@Override
	public boolean dispatchKeyEvent(KeyEvent event) {
		if (event.getKeyCode() == KeyEvent.KEYCODE_BACK
				&& event.getAction() == KeyEvent.ACTION_DOWN) {
			switch (keyBackClickCount++) {
			case 0:
				Toast.makeText(this,
						getResources().getString(R.string.press_again_exit),
						Toast.LENGTH_SHORT).show();
				Timer timer = new Timer();
				timer.schedule(new TimerTask() {
					@Override
					public void run() {
						keyBackClickCount = 0;
					}
				}, 3000);
				break;
			case 1:
				finish();
				break;
			default:
				break;
			}
			return true;
		}
		return super.dispatchKeyEvent(event);
	}

}
